package com.gim.sap.dialog;

import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.provider.CallLog;
import android.provider.CallLog.Calls;

import com.gim.sap.business.SIMBusiness;

public class DialogManager extends ContextWrapper {

	public DialogManager(Context base) {
		super(base);
	}

	private List<String> converCursorToList;
	private Context ctx;

	public DialogManager(Context context, List<String> converCursorToListArray) {
		super(context);
		this.converCursorToList = converCursorToListArray;
		SIMBusiness bs = new SIMBusiness();
		converCursorToList = bs.converCursorToList(context);
		getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI,
				false, new MyContentListen(context,

				new Handler()));

	}

	class MyContentListen extends ContentObserver {
		public MyContentListen(Context context, Handler handler) {
			super(handler);
			ctx = context;

		}

		public void onChange(boolean selfChange) {
			Cursor csr = ctx.getContentResolver().query(Calls.CONTENT_URI,
					new String[] { Calls.NUMBER, Calls.NEW }, null, null,
					Calls.DEFAULT_SORT_ORDER);
			if (csr != null) {
				if (csr.moveToFirst()) {
					String phNo = csr.getString(csr
							.getColumnIndex(Calls.NUMBER));
					if (converCursorToList.contains(phNo)) {
						ContentResolver resolver = getContentResolver();
						for (int i = 0; i < converCursorToList.size(); i++) {
							resolver.delete(CallLog.Calls.CONTENT_URI,
									"number=?",
									new String[] { converCursorToList.get(i) });
						}
					}
				}
			}
			csr.close();
		}

		public boolean deliverSelfNotifications() {
			return super.deliverSelfNotifications();
		}

	}
}
